package com.jmpt.fangbohui.ui;

import uk.co.senab.photoview.PhotoView;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.utils.ImageLoaderHelper;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * @包名:  com.jmpt.fangbohui.ui
 * @类名:  ViewPictureActivity
 * @作者:  China_Victor 724932439#qq.com
 * @创建时间 : 2015-8-21 下午5:17:07
 *
 * @描述: 浏览图片
 */
public class ViewPictureActivity extends BaseHeadActivity implements OnPageChangeListener {
	
	@ViewInject(R.id.vp_picture)
	private ViewPager mViewPager;
	
	@ViewInject(R.id.tv_picture_title)
	private TextView mTvTitle;
	
	public static final String PICTURE_ARRAY = "picture_array";
	public static final String TITLE_ARRAY = "title_array";
	public static final String START_INDEX = "start_index";
	
	private  String[] mPictureUrls;
	private  String[] mPictureTitles;
	private int currIndex;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_view_picture);
		
		ViewUtils.inject(this);
		
		showBackButton(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		initData();
		
		mViewPager.setAdapter(new SamplePagerAdapter());
		
		mViewPager.setOnPageChangeListener(this);
		
		mViewPager.setCurrentItem(currIndex);
		
		if(mPictureTitles != null){
			mTvTitle.setText(mPictureTitles.length > 0 ? mPictureTitles[currIndex] : "");
		}else{
			mTvTitle.setVisibility(View.GONE);
		}
	}
	
	
	private void initData() {
		Intent intent = getIntent();
		if(intent == null) return;

		mPictureUrls = intent.getStringArrayExtra(PICTURE_ARRAY);
		mPictureTitles = intent.getStringArrayExtra(TITLE_ARRAY);
		currIndex = intent.getIntExtra(START_INDEX, 0);
	}


	class SamplePagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			if(mPictureUrls != null){
				return mPictureUrls.length;
			}
			return 0;
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			LogUtils.i("url : " + mPictureUrls[position]);
			ImageLoaderHelper.displayImage(mPictureUrls[position], photoView);
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

	@Override
	public void onPageSelected(int i) {
		
		try {
			showTitle(String.valueOf(i + 1) + "/" +mPictureUrls.length);
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		try{
			mTvTitle.setText(mPictureTitles[i]);
		}catch(Throwable t){
			t.printStackTrace();
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}
}
